<template>
  <view>
    <block v-if="managers.length > 0">
      <uni-list
        :border="false"
        class="manage-detail-list"
        v-for="(item, managerIndex) in managers"
        :key="managerIndex"
      >
        <uni-list-item>
          <view slot="header" class="slot-box">
            <view class="header-text">配置人：{{ item.nickName }}</view>
          </view>
        </uni-list-item>
        <template v-if="item.toSysDepts && item.toSysDepts.length > 0">
          <view v-for="dept in item.toSysDepts" :key="dept.deptId">
            <uni-list-item
              thumb="../../static/dept.png"
              thumb-size="lg"
              :title="dept.deptName"
            >
              <template slot="footer">
                <button
                  class="footer-remove-btn"
                  @click="
                    remove(dept.deptName, managerIndex, dept.deptId, null)
                  "
                >
                  删除
                </button>
              </template></uni-list-item
            >
          </view>
        </template>
        <template v-if="item.toSysUsers && item.toSysUsers.length > 0">
          <view v-for="user in item.toSysUsers" :key="user.userName">
            <uni-list-item
              :thumb="user.avatar"
              thumb-size="lg"
              :title="user.nickName"
            >
              <template slot="footer">
                <button
                  class="footer-remove-btn"
                  @click="
                    remove(user.nickName, managerIndex, null, user.userName)
                  "
                >
                  删除
                </button>
              </template></uni-list-item
            >
          </view>
        </template>
      </uni-list>
      <view class="null-content"></view>
      <view class="footer-btn">
        <view class="to-add" @tap="removeAll">全部删除</view>
      </view>
    </block>
  </view>
</template>

<script>
import uniList from "../../components/uni-list/uni-list";
import uniListItem from "../../components/uni-list-item/uni-list-item";
import uniIcons from "../../components/uni-icons/uni-icons";
import {
  findMyPower,
  delectPower,
  delectAllPower,
  savePowerSource,
} from "../../common/service.js";
export default {
  components: {
    uniList,
    uniListItem,
    uniIcons,
  },
  data() {
    return {
      managers: [],
      loginInfo: {},
    };
  },
  onLoad(e) {
    if (e.loginInfo) {
      this.loginInfo = JSON.parse(e.loginInfo);

      if (
        this.loginInfo &&
        this.loginInfo.toPowerId &&
        this.loginInfo.powerModule
      ) {
        this.getData();
      }
    }
  },
  methods: {
    getData() {
      uni.showLoading({
        title: "加载中",
      });
      findMyPower(
        {
          userName: this.loginInfo.toPowerId,
          powerModule: this.loginInfo.powerModule,
        },
        (res) => {
          uni.hideLoading();
          if (res && res.data && res.data.code == 200) {
            if (res.data.data) {
              let list = res.data.data;
              this.managers = list;
            }
          } else {
            uni.showToast({
              title: "获取我管理的权限失败",
              duration: 2000,
            });
          }
        },
        (err) => {
          uni.showToast({
            title: "操作失败",
            duration: 2000,
          });
        }
      );
    },
    remove(name, managerIndex, powerDeptId, powerUserId) {
      let self = this;
      if (!self.loginInfo.parentUserId || !self.loginInfo.toPowerId) {
        uni.showToast({
          title: "登录信息为空",
        });
        return;
      }
      const parentUserId = self.managers[managerIndex].userName;
      const toPowerId = self.loginInfo.toPowerId;
      const powerModule = self.loginInfo.powerModule;

      uni.showModal({
        title: "提示",
        content: `确认删除 ${name} 此权限吗`,
        success: function (res) {
          if (res.confirm) {
            console.log("用户点击确定");
            uni.showLoading({
              title: "删除中",
            });
            const data = {
              parentUserId: parentUserId, // 父级授权人
              toPowerId: toPowerId, //   授权对象
              powerDeptId: powerDeptId || "", //被授权的部门id（删除用户权限此项传空）
              powerUserId: powerUserId || "", // 被授权的对象id（删除部门权限此项传空）
              powerModule: powerModule, // 权限模块（巡店固定值：XD_RESULT）
            };
            delectPower(
              data,
              (res) => {
                if (res && res.data && res.data.code == 200) {
                  uni.showToast({
                    title: "删除成功",
                    duration: 2000,
                  });
                  setTimeout(() => {
                    self.getData();

                    savePowerSource(
                      { userNames: toPowerId },
                      (res) => {
                        console.log(res.data);
                      },
                      (err) => {
                        console.log(err);
                      }
                    );
                  }, 2000);
                } else {
                  uni.showToast({
                    title: "删除失败",
                    duration: 2000,
                  });
                }
              },
              (err) => {
                uni.showToast({
                  title: "删除失败",
                  duration: 2000,
                });
              }
            );
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    removeAll() {
      let self = this;
      if (!self.loginInfo.parentUserId || !self.loginInfo.toPowerId) {
        uni.showToast({
          title: "登录信息为空",
        });
        return;
      }
      const parentUserId = self.loginInfo.parentUserId;
      const toPowerId = self.loginInfo.toPowerId;
      const powerModule = self.loginInfo.powerModule;
      uni.showModal({
        title: "提示",
        content: "确认删除我管理的权限吗",
        success: function (res) {
          if (res.confirm) {
            console.log("用户点击确定");
            uni.showLoading({
              title: "删除中",
            });
            const data = {
              parentUserId: parentUserId, // 父级授权人
              toPowerId: toPowerId, //   授权对象
              powerModule: powerModule || "XD_RESULT", // 权限模块（巡店固定值：XD_RESULT）
            };
            delectAllPower(
              data,
              (res) => {
                if (res && res.data && res.data.code == 200) {
                  uni.showToast({
                    title: "删除成功",
                    duration: 2000,
                  });
                  setTimeout(() => {
                    self.getData();
                    self.managers = [];
                    savePowerSource(
                      { userNames: self.loginInfo.toPowerId },
                      (res) => {
                        console.log(res.data);
                      },
                      (err) => {
                        console.log(err);
                      }
                    );
                  }, 2000);
                } else {
                  uni.showToast({
                    title: "删除失败",
                    duration: 2000,
                  });
                }
              },
              (err) => {
                uni.showToast({
                  title: "删除失败",
                  duration: 2000,
                });
              }
            );
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
  },
};
</script>

<style scoped>
page {
  background: #f6f6f6;
}
.manage-detail-list {
  margin-bottom: 30upx;
}
.footer-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
}
.to-add {
  flex: 1;
  line-height: 100rpx;
  text-align: center;
  font-size: 30rpx;
  font-weight: bold;
  color: #ffffff;
  background: #3875c6;
  border-radius: 6upx;
  margin: 30upx 20upx;
}
.null-content {
  height: 160upx;
}
.footer-remove-btn {
  font-weight: 500;
  color: #3875c6;
  font-size: 32upx;
  background: transparent;
}
.footer-remove-btn:after {
  border: none;
}
</style>
